<template>
  <div>
    <ZgPage
      ref="page"
      title="定时任务管理"
      addBtnName="新增任务"
      :addOpt="{title:'新增任务'}"
      :updateOpt="{title:'修改任务'}"
      :extraBtns="extraBtns"
      @search="onSearch"
      @del="onDel"
      @add="onAdd"
      @update="onUpdate"
      @extraBtn="onExtraBtnClick"
    >
      <template slot="columns">
        <a-table-column :width="150" dataIndex="name" title="任务名称"></a-table-column>
        <a-table-column :width="150" dataIndex="code" title="任务编码"></a-table-column>
        <a-table-column :width="150" dataIndex="cron" title="cron表达式"></a-table-column>
        <a-table-column dataIndex="uri" title="任务URI"></a-table-column>
        <a-table-column :width="100" title="状态">
          <template slot-scope="text,record">
            <a-tag size="small">{{ record.state }}</a-tag>
          </template>
        </a-table-column>
      </template>
      <template slot="add">
        <Add ref="add"></Add>
      </template>
      <template slot="update" slot-scope="scope">
        <Update ref="update" :jsonStr="scope.jsonStr"></Update>
      </template>
    </ZgPage>
  </div>
</template>
<script>
import Add from './Add'
import Update from './Update'
export default {
  components: {
    Add, Update
  },
  data () {
    return {
      extraBtns: [
        { key: 1, name: '上线', icon: 'fa-arrow-up', isShow: obj => { return obj.state === '下线' } },
        { key: 2, name: '下线', icon: 'fa-arrow-down', isShow: obj => { return obj.state === '运行' } },
        { key: 3, name: '手动执行', icon: 'fa-hand-pointer-o', isShow: obj => { return true } }
      ]
    }
  },
  methods: {
    onSearch (param) {
      this.$http.get('/zg-job/job/page', param, data => {
        this.$refs.page.endSearch(
          {
            total: parseInt(data.total),
            list: data.records.map(t => {
              t.key = t.id
              return t
            })
          }
        )
      })
    },
    onDel (ids) {
      this.$http.post('/zg-job/job/del', ids, () => {
        this.$refs.page.search()
      })
    },
    onAdd () {
      const result = this.$refs.add.save()
      if (!result.isOk) {
        this.$error({ title: result.msg })
        return
      }
      this.$http.post('/zg-job/job/add', result.data, () => {
        this.$refs.page.hideAdd()
        this.$refs.page.search()
      })
    },
    onUpdate () {
      const result = this.$refs.update.save()
      if (!result.isOk) {
        this.$error({ title: result.msg })
        return
      }
      this.$http.post('/zg-job/job/update', result.data, () => {
        this.$refs.page.hideUpdate()
        this.$refs.page.search()
      })
    },
    onExtraBtnClick (param) {
      if (param.key === 1) {
        this.$http.post('/zg-job/job/start', param.obj.id, () => {
          this.$refs.page.hideUpdate()
          this.$refs.page.search()
        })
      } else if (param.key === 2) {
        this.$http.post('/zg-job/job/stop', param.obj.id, () => {
          this.$refs.page.hideUpdate()
          this.$refs.page.search()
        })
      } else if (param.key === 3) {
        this.$http.post('/zg-job/job/go', param.obj.id, () => {
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
